
using Microsoft.EntityFrameworkCore;

using Identity.Interface;
using Identity.Models;
using Identity.Contexts;
using BootstrapBlazor.Components;

namespace Identity.Service;
/// <summary>
/// 
/// </summary>
/// <param name="dbFactory"></param>
public class RoleService(IDbContextFactory<SysContext> dbFactory) : IRole
{

    private IDbContextFactory<SysContext> DbFactory { get; } = dbFactory;

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public List<Role> GetAll()
        {
         
                using var dbcontext = DbFactory.CreateDbContext();

                return dbcontext.Roles.ToList();
      

        }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="groupId"></param>
    /// <returns></returns>



    /// <summary>
    /// 
    /// </summary>
    /// <param name="menuId"></param>
    /// <returns></returns>
    public List<string> GetRolesByMenuId(string? menuId)
    {
        using var dbcontext = DbFactory.CreateDbContext();

        return dbcontext.Navigationroles.Where(s => s.NavigationId == menuId).Select(s => s.RoleId!).AsNoTracking().ToList();
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="menuId"></param>
    /// <param name="roleIds"></param>
    /// <returns></returns>
    public bool SaveRolesByMenuId(string? menuId, IEnumerable<string> roleIds)
    {
        using var dbcontext = DbFactory.CreateDbContext();
        var ret = false;
        try
        {
            dbcontext.Database.ExecuteSqlRaw("delete from NavigationRole where NavigationID = {0}", menuId!);
            dbcontext.AddRange(roleIds.Select(g => new  NavigationRole { RoleId = g, NavigationId = menuId }));
            dbcontext.SaveChanges();
            ret = true;
        }
        catch (Exception)
        {
            throw;
        }
        return ret;
    }

    public List<string> GetRolesByGroupId(string? groupId)
    {
        throw new NotImplementedException();
    }

    public bool SaveRolesByGroupId(string? groupId, IEnumerable<string> roleIds)
    {
        throw new NotImplementedException();
    }

    public List<string> GetRolesByUserId(string? userId)
    {
   
            using var dbcontext = DbFactory.CreateDbContext();

            return dbcontext.UserRoles.Where(s => s.UserId == userId).Select(s => s.RoleId!).AsNoTracking().ToList();

    }

   public bool SaveRolesByUserId(string? userId, IEnumerable<string> roleIds)
 {
     using var dbcontext = DbFactory.CreateDbContext();
     var ret = false;
     try
     {
         dbcontext.Database.ExecuteSqlRaw("delete from UserRoles where UserID = {0}", userId!);
         dbcontext.AddRange(roleIds.Select(g => new  UserRole { RoleId = g, UserId = userId }));
         dbcontext.SaveChanges();
         ret = true;
     }
     catch (Exception)
     {
         throw;
     }
     return ret;
 }
public  async Task<bool> SaveAsync(Role model, ItemChangedType changedType)
 {
     var context = DbFactory.CreateDbContext();
     if (changedType == ItemChangedType.Add)
     {  
        model.Id=  Guid.NewGuid().ToString();
        context.Entry(model).State = EntityState.Added;
 
     }
     else
     {
         context.Entry(model).State = EntityState.Modified;
     }
     return await context.SaveChangesAsync() > 0;
 }
     public  async Task<bool> DeleteAsync(IEnumerable<Role> models)
    {
        // 通过模型获取主键列数据
        // 支持批量删除
        var context = DbFactory.CreateDbContext();
        context.RemoveRange(models);
        return await context.SaveChangesAsync() > 0;
    }

 
}
